﻿using System;
using System.Disposables;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using SoftSource.Common;
using SoftSource.MVVMC;
using SoftSource.Reactive;
using SoftSource.Timecard.Entry.ViewModelInterfaces;
using SoftSource.Timecard.Web.Data;

namespace SoftSource.Timecard.Entry.Views
{
    public partial class EntryEditorView : UserControl, IDisposable, IView<IEntryEditorViewModelConsume>
    {
        #region Fields
        private CompositeDisposable compositeDisposable;
        #endregion Fields

        public EntryEditorView()
        {
            InitializeComponent();

            this.compositeDisposable = new CompositeDisposable();

            Loaded += new RoutedEventHandler(EntryEditorView_Loaded);
        }
        
        #region Events

        private void EntryEditorView_Loaded(object sender, RoutedEventArgs e)
        {
            if (this.ViewModel != null)
            {
                this.ViewModel.Load // Load the data required to use this ViewModel
                    .Nullable()
                    .Do(unit => this.SubscribeToEntry(this.ViewModel.Entry))
                    .Do(unit => this.DescriptionTextBox.Focus())
                    .Subscribe();
            }
        }

        private void SubscribeToEntry(EntryLog entry)
        {
            this.compositeDisposable.Add(entry.WhenPropertyChanged()
                .Where(evnt => evnt.EventArgs.PropertyName == ObjectReflection.GetPropertyName(() => entry.ProjectID))
                .Do(evnt => entry.CategoryID = App.Model.ProjectSummaries.Entities
                    .Single(p => p.ProjectID == entry.ProjectID).ProjectCategorySummaries
                    .OrderBy(pc => pc.CategorySummary.Name)
                    .First().CategoryID)
                .Subscribe());
        }

        private void Save_Click(object sender, RoutedEventArgs e)
        {
            var oldHours = this.ViewModel.Entry.Duration;
            this.ViewModel.Save.Subscribe();
        }

        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            this.ViewModel.Cancel();
        }

        private void Clear_Click(object sender, RoutedEventArgs e)
        {
            this.ViewModel.Entry.Description = string.Empty;
            this.ViewModel.Entry.Duration = 0;
            this.ViewModel.Entry.EntryDate = DateTime.Now;
            this.ViewModel.Entry.OvernightStay = false;
        }

        private void EntryStackPanel_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                this.ViewModel.Entry.EntryDate = this.EntryDatePicker.SelectedDate ?? DateTime.Now;
                this.ViewModel.Entry.Duration = decimal.Parse(this.HoursTextBox.Text);
                this.ViewModel.Entry.OvernightStay = this.OvernightStayCheckBox.IsChecked ?? false;
                this.ViewModel.Entry.Description = this.DescriptionTextBox.Text;

                this.Save_Click(null, null);
            }
            else if (e.Key == Key.Escape)
            {
                this.ViewModel.Cancel();
            }
        }

        #endregion Events

        #region IView<IEntryEditorViewModelConsume> Members

        public IEntryEditorViewModelConsume ViewModel
        {
            get { return this.DataContext as IEntryEditorViewModelConsume; }
            set { this.DataContext = value; }
        }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            this.compositeDisposable.Dispose();
            GC.SuppressFinalize(this);
        }

        #endregion

        ~EntryEditorView()
        {
            this.Dispose();
        }
    }
}
